import { http } from "@/utils/http";

export type PredepositInfo = {
  id: number | null;
  name: string;
  payerId: number;
  payerType: number;
  phone: string;
  email: string;
  amount: number; // 当前余额
};

export type QueryPreParams = {
  payerId: number;
  payerType: number;
};

export type PredepositResult = {
  code: number;
  data: PredepositInfo;
  message: string;
};

export type MutatePreBody = {
  payerId: number;
  payerType: number;
  amount: number;
};

export type BaseResult = { code: number; message: string };

// 预存列表项（与 PredepositInfo 一致，这里单独声明以便后续扩展）
export type PredepositListItem = PredepositInfo;

export type PredepositListParams = {
  page: number; // 后端从0开始
  limit: number;
  name: string;
};

export type PredepositListResponse = {
  code: number;
  data: {
    total: number;
    list: PredepositListItem[];
    pageNum: number;
    pageSize: number;
  };
  message: string;
};

export type DeletePredepositBody = {
  ids: number[];
};

export type TotalAmountResponse = {
  code: number;
  data: number;
  message: string;
};

// 查询预存款信息
export const queryPredeposit = (params: QueryPreParams) =>
  http.request<PredepositResult>(
    "post",
    "/SpPredepositController/querybypayer",
    { data: params }
  );

// 新增预存款信息
export const addPredeposit = (body: MutatePreBody) =>
  http.request<BaseResult>("post", "/SpPredepositController/add", {
    data: body
  });

// 充值（更新余额）
export const rechargePredeposit = (body: MutatePreBody) =>
  http.request<BaseResult>("put", "/SpPredepositController/update", {
    data: body
  });

// 获取预存列表（分页）
export const getPredepositList = (params: PredepositListParams) =>
  http.request<PredepositListResponse>(
    "post",
    "/SpPredepositController/payerlist",
    { data: params }
  );

// 删除与批量删除
export const deletePredeposit = (ids: number[]) =>
  http.request<BaseResult>("delete", "/SpPredepositController/delete", {
    data: { ids } as DeletePredepositBody
  });

// 获取预存总金额
export const getPredepositTotalAmount = () =>
  http.request<TotalAmountResponse>(
    "get",
    "/SpPredepositController/totalamount"
  );
